package com.hdoit.apollo.web.shop.apis;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.mapper.BillingMapper;
import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.purchase.mapper.PurchaseMapper;
import com.hdoit.apollo.logical.purchase.model.Purchase;
import com.hdoit.apollo.logical.purchase.service.IPurchaseService;
import com.hdoit.apollo.logical.shop.model.Payment;
import com.hdoit.apollo.logical.shop.service.IPaymentService;
import com.hdoit.apollo.logical.viewmodel.billing.BillingSearch;
import com.hdoit.apollo.logical.viewmodel.purchase.PurchaseSearch;
import com.hdoit.apollo.logical.viewmodel.shop.GatheringStat;
import com.hdoit.apollo.logical.viewmodel.shop.PayStat;
import com.hdoit.apollo.logical.viewmodel.shop.PaymentSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.message.PagerModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/api/v1/payment/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/payment")
public class PaymentApi extends BaseApi{
	
	@Autowired
	private IPaymentService paymentService;
	
	@Autowired
	private BillingMapper billingMapper;
	
	@Autowired
	private IPurchaseService purchaseService;
	
	@Autowired
	private PurchaseMapper purchaseMapper;
	
	private final String savePaymentApi="savePayment";
	private final String getPaymentByScroll="getPaymentByScroll";
	private final String getPaymentById="getPaymentById";
	private final String queryAccountDetailApi="queryAccountDetail";
	private final String queryGatheringListByScrollApi="queryGatheringListByScroll";
	
	private final String queryPaymentListByScrollApi="queryPaymentListByScroll";
	
	private final String queryGatheringStatApi="queryGatheringStat";
	private final String queryPaymentStatApi="queryPaymentStat";
	
	@RequestMapping(value = queryPaymentStatApi)
    @ApiOperation(notes = queryPaymentStatApi, httpMethod = "POST", value = "账本收支")
    public @ResponseBody String queryPaymentStat(PurchaseSearch search){
    	ResponseModel rm=new ResponseModel();
    	 PayStat payStat=purchaseMapper.queryPayStat(search);
    	 rm.put("payStat",payStat);
    	return rm.toJson();
    }
	
	@RequestMapping(value = queryGatheringStatApi)
    @ApiOperation(notes = queryGatheringStatApi, httpMethod = "POST", value = "账本收支")
    public @ResponseBody String queryGatheringStat(BillingSearch search){
    	ResponseModel rm=new ResponseModel();
    	GatheringStat gatheringStat=billingMapper.queryGatheringStat(search);
    	rm.put("gatheringStat",gatheringStat);
    	return rm.toJson();
    }
	
	@RequestMapping(value = queryGatheringListByScrollApi)
    @ApiOperation(notes = queryGatheringListByScrollApi, httpMethod = "POST", value = "滚动查找应收款列表")
    public @ResponseBody String queryGatheringListByScroll(BillingSearch search,int dataPosition,int pageSize){
		search.setType("notShipped");
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	
    	List<Billing> billings= billingMapper.queryListMPForPc(search, pagerModel);
    	rm.put("billings",billings);
    	
    	return rm.toJson();
    }
	
	@RequestMapping(value = queryPaymentListByScrollApi)
    @ApiOperation(notes = queryPaymentListByScrollApi, httpMethod = "POST", value = "滚动查找应付款列表")
    public @ResponseBody String queryPaymentListByScroll(PurchaseSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	 List<Purchase> purchases= purchaseService.queryListMP(search,pagerModel);
    	
    	rm.put("purchases",purchases);
    	
    	return rm.toJson();
    }
	
	
    @RequestMapping(value = savePaymentApi)
    @ApiOperation(notes = savePaymentApi, httpMethod = "POST", value = "保存客户付款信息")
    public @ResponseBody String savePayment(String paymentsJson,String customerId){
    	@SuppressWarnings("unchecked")
    	List<Payment> payments=(List<Payment>) JsonUtils.readValues(paymentsJson, List.class, Payment.class);
    	
    	ResponseModel rm=new ResponseModel();
    	if(!paymentService.applyPayment(payments,customerId)){
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }
    
 	@RequestMapping(value = getPaymentByScroll)
    @ApiOperation(notes = getPaymentByScroll, httpMethod = "POST", value = "滚动查找获取付款列表")
    public @ResponseBody String getPaymentByScroll(PaymentSearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}
    	List<Payment> payments = paymentService.queryListMP(search,pagerModel);
    	rm.put("payments",payments);
    	return rm.toJson();
    }
 	
 	@RequestMapping(value = getPaymentById)
    @ApiOperation(notes = getPaymentById, httpMethod = "POST", value = "通过Id查找Payment")
    public @ResponseBody String getPaymentById(String paymentId){
    	ResponseModel rm=new ResponseModel();
    	Payment payment = paymentService.queryModelById(paymentId);
    	rm.put("payment",payment);
    	return rm.toJson();
    }
 	
 	@RequestMapping(value = queryAccountDetailApi)
    @ApiOperation(notes = queryAccountDetailApi, httpMethod = "POST", value = "账本收支")
    public @ResponseBody String queryAccountDetail(PaymentSearch search){
    	ResponseModel rm=new ResponseModel();
    	List<Payment> payments = paymentService.queryAccountDetail(search);
    	rm.put("payments",payments);
    	return rm.toJson();
    }
 	
}
